package com.irate.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Dialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.widget.Toast;

import com.irate.activity.R;
import com.irate.http.AsyncHttpResponseHandler;
import com.irate.model.FriendRequest;
import com.irate.provider.Constant;
import com.irate.service.AsyncHTTPRest;

public class FriendRequestDAO {

	public List<FriendRequest> parseData(String json, String regex) {
		List<FriendRequest> lst = new ArrayList<FriendRequest>();

		try {

			JSONObject obj = new JSONObject(json);
			JSONArray array = obj.getJSONArray("message");

			for (int i = 0; i < array.length(); i++) {
				FriendRequest f = new FriendRequest();

				JSONObject fobj = array.getJSONObject(i);
				f.setFriend_request_id(fobj.getString("friend_request_id"));
				f.setFriend_request_date(fobj.getString("friend_request_date"));
				try {
					f.setUser_id_request_to(fobj
							.getString("user_id_request_to"));
				} catch (Exception e) {
					f.setUser_id_request_from(fobj
							.getString("user_id_request_from"));
				}

				try {
					f.setUser_photo(fobj.getString("user_photo"));
				} catch (Exception e) {

				}

				f.setUser_name(fobj.getString("user_name"));
				f.setUser_firstname(fobj.getString("user_firstname"));
				f.setUser_lastname(fobj.getString("user_lastname"));
				f.setIsMock(false);

				lst.add(f);
			}

		} catch (Exception e) {

		}

		return lst;
	}

	public void getListSendRequest(AsyncHttpResponseHandler handler,
			Context context, Dialog dialog) {

		ConnectivityManager con = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (con == null || con.getActiveNetworkInfo() == null
				|| !con.getActiveNetworkInfo().isConnected()) {

			
			Toast.makeText(context, context.getResources().getString(R.string.connection_error),
					Toast.LENGTH_LONG).show();
			if( dialog!= null){
				dialog.dismiss();
			}
		} else {
			String link = Constant.URL + "/" + Constant.SERVICE_CATEGORY_FRIEND
					+ "/" + Constant.SERVICE_SEND_REQUEST_FRIEND;

			Header[] headers = { new BasicHeader("X-API-KEY", Constant.XAPIKEY) };

			AsyncHTTPRest.post(link, headers, null, handler);
		}

	}

	public void getListIsRequest(AsyncHttpResponseHandler handler,
			Context context, Dialog dialog) {

		ConnectivityManager con = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (con == null || con.getActiveNetworkInfo() == null
				|| !con.getActiveNetworkInfo().isConnected()) {
			
			Toast.makeText(context,  context.getResources().getString(R.string.connection_error),
					Toast.LENGTH_LONG).show();
			
			if( dialog!= null){
				dialog.dismiss();
			}
			
		} else {
			String link = Constant.URL + "/" + Constant.SERVICE_CATEGORY_FRIEND
					+ "/" + Constant.SERVICE_IS_REQUEST_FRIEND;

			Header[] headers = { new BasicHeader("X-API-KEY", Constant.XAPIKEY) };

			AsyncHTTPRest.post(link, headers, null, handler);
		}
	}

	public void acceptFriendRequest() {

	}

	public void denyFriendRequest() {

	}
}
